package com.slwish.xty.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.utils.IntentUtils;
import com.slwish.xty.R;
import com.slwish.xty.adapter.CreditLifeAdapter;
import com.slwish.xty.entity.CreditLifeModel;
import com.slwish.xty.mvp.contract.CreditLifeContract;
import com.slwish.xty.mvp.presenter.CreditLifePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 信用生活
 */
public class CreditLifeActivity extends PresenterToolbarActivity<CreditLifeContract.Presenter> implements CreditLifeContract.View {

    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    CreditLifeAdapter mAdapter;
    List<CreditLifeModel> mLists = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.act_credit_life;
    }

    @Override
    protected CreditLifeContract.Presenter initPresenter() {
        return new CreditLifePresenter(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.credit_life_title);
        xrecyclerview.setLayoutManager();
        xrecyclerview.setLoadingMoreEnabled(false);
        xrecyclerview.setPullRefreshEnabled(false);
        mAdapter = new CreditLifeAdapter(mLists);
        xrecyclerview.setAdapter(mAdapter);
        mPresenter.getAccommodationInfo(mContext);

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mAdapter.setOnItemsClickListener(new CreditLifeAdapter.OnItemsClickListener() {
            @Override
            public void onItemsClick(BaseQuickAdapter adapter, View view, int parentPosition, int position) {
                if (parentPosition < 1) return;
                Bundle bundle = new Bundle();
                bundle.putString("URL", mLists.get(parentPosition - 1).getList().get(position).getUrl());
                IntentUtils.startActivity(mContext, WebViewActivity.class, bundle);
            }
        });
    }

    /**
     * 数据
     *
     * @param model
     */
    @Override
    public void getInfoSuccess(List<CreditLifeModel> model) {
        if (model != null && model.size() > 0) {
            mLists.clear();
            mLists.addAll(model);
            mAdapter.notifyDataSetChanged();
        } else {
            View view = View.inflate(mContext, R.layout.layout_empty, null);
            mAdapter.setEmptyView(view);
        }


    }
}
